<?php

    /**
     * Cron
     *
     * @package      Core
     * @subpackage   Cron
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Cron
    {

    ////////////////////////////////////////////////////////////////////////////
    //                             Static Methods                             //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Parses all the CRON file
         *
         * @param       boolean     $reset
         * @throws      Core_Exception
         * @throws      Core_ArgumentException
         * @throws      Core_ArgumentNullException
         * @return      run
         */
        public static function run($reset = false)
        {
            $tasks = Core_Cron_Compiler::get();

            // Run tasks
            foreach ($tasks as $task) {
                $classname = 'Core_Cache_Generator_'. ucfirst(strtolower($task['type'])) .'CacheGenerator';
                $cache     = new $classname($task['module'], $task['name']);

                if ($reset === false) {
                    if (Core_IO_File::exists($cache->getClassPath())
                        &&
                        (time() - call_user_func($cache->getClassName() .'::getCacheTimeStamp') <= call_user_func($cache->getClassName() .'::getCacheLifeSpan'))
                    ) {
                        continue;
                    }
                }

                $cache->setComment($task['comment']);
                $cache->setCacheLifeSpan((int)$task['lifespan']);

                $data = call_user_func_array($task['action'], $task['parameters']);

                if ($cache instanceof Core_Cache_Generator_ArrayCacheGenerator) {
                    $cache->setKey($task['key']);
                    $cache->setData((array)$data);
                }

                $cache->generate();
            }

            // Garbage collector
            unset($tasks);
        }
    }